#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const int N=2e5+10;
int a[N];
void solve()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	ll ans=0;
	ll cnt1=0,cnt2=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]==1)
		{
			cnt1++;
			ans+=cnt2;
		}
		else if(a[i]==2)
		{
			cnt2++;
			ans+=cnt1;
		}
	}
	
	sort(a+1,a+1+n);
	ll cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]!=a[i-1])
		{
			ans+=(cnt*(cnt-1)/2);
			cnt=1;
		}
		else cnt++;
	}
	ans+=(cnt*(cnt-1)/2);
	cout<<ans<<endl;
}
int main()
{
	IOS;
	int T=1;
	cin>>T;

	while(T--)
	{
		solve();
	}

	return 0;
}

